package com.ruoyi.common.utils;

import com.alibaba.fastjson.JSON;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.apache.commons.codec.binary.Base64.encodeBase64;

public class idMainUtils {

    //API产品路径
    private static final String REQUESTURL = "https://personcard.market.alicloudapi.com/ai_market/ai_ocr_universal/shen_fen_zheng/ch/v1";
    //阿里云APPCODE
    private static final String APPCODE = "9dfa574365584bf78c692fac28752839";

    public static void main(String[] args) throws IOException {
        //API产品路径
        String requestUrl = "https://personcard.market.alicloudapi.com/ai_market/ai_ocr_universal/shen_fen_zheng/ch/v1";
        //阿里云APPCODE
        String appcode = "9dfa574365584bf78c692fac28752839";

        CloseableHttpClient httpClient = null;
        try{
            httpClient = HttpClients.createDefault();
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // 装填参数
            if (true) {
                //启用BASE64编码方式进行识别
                //内容数据类型是BASE64编码
                String imgFile = "C:/Users/Administrator/Desktop/ww.jpg";
                String imgBase64 = "";
                try {
                    File file = new File(imgFile);
                    byte[] content = new byte[(int) file.length()];
                    FileInputStream finputstream = new FileInputStream(file);
                    finputstream.read(content);
                    finputstream.close();
                    imgBase64 = new String(encodeBase64(content));
                } catch (IOException e) {
                    e.printStackTrace();
                    return;
                }
                params.add(new BasicNameValuePair("IMAGE", imgBase64));
                params.add(new BasicNameValuePair("IMAGE_TYPE", "0"));
            } else {
                //启用URL方式进行识别
                //内容数据类型是图像文件URL链接
                params.add(new BasicNameValuePair("IMAGE", "https://icredit-api-market.oss-cn-hangzhou.aliyuncs.com/%E8%BA%AB%E4%BB%BD%E8%AF%81_%E5%8F%8D%E9%9D%A2.jpg"));
                params.add(new BasicNameValuePair("IMAGE_TYPE", "1"));
            }

            // 创建一个HttpPost实例
            HttpPost httpPost = new HttpPost(requestUrl);
            httpPost.addHeader("Authorization","APPCODE " + appcode);
            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

            // 设置请求参数
            httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));

            // 发送POST请求
            HttpResponse execute = httpClient.execute(httpPost);

            // 获取状态码
            int statusCode = execute.getStatusLine().getStatusCode();
            System.out.println(statusCode);

            // 获取结果
            HttpEntity entity = execute.getEntity();
            String result = EntityUtils.toString(entity, "utf-8");
            System.out.println(result);
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


//    public String identityAuthentication() {
//        CloseableHttpClient httpClient = null;
//        String result = "";
//        try {
//            httpClient = HttpClients.createDefault();
//            List<NameValuePair> params = new ArrayList<NameValuePair>();
//            // 装填参数
//            if (false) {
//                //启用BASE64编码方式进行识别
//                //内容数据类型是BASE64编码
//                String imgFile = "图片路径";
//                String imgBase64 = "";
//                try {
//                    File file = new File(imgFile);
//                    byte[] content = new byte[(int) file.length()];
//                    FileInputStream finputstream = new FileInputStream(file);
//                    finputstream.read(content);
//                    finputstream.close();
//                    imgBase64 = new String(encodeBase64(content));
//                } catch (IOException e) {
//                    e.printStackTrace();
//                    return null;
//                }
//                params.add(new BasicNameValuePair("IMAGE", imgBase64));
//                params.add(new BasicNameValuePair("IMAGE_TYPE", "0"));
//            } else {
//                //启用URL方式进行识别
//                //内容数据类型是图像文件URL链接
//                params.add(new BasicNameValuePair("IMAGE", "https://icredit-api-market.oss-cn-hangzhou.aliyuncs.com/%E8%BA%AB%E4%BB%BD%E8%AF%81_%E5%8F%8D%E9%9D%A2.jpg"));
//                params.add(new BasicNameValuePair("IMAGE_TYPE", "1"));
//            }
//
//            // 创建一个HttpPost实例
//            HttpPost httpPost = new HttpPost(REQUESTURL);
//            httpPost.addHeader("Authorization", "APPCODE " + APPCODE);
//            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
//
//            // 设置请求参数
//            httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
//
//            // 发送POST请求
//            HttpResponse execute = httpClient.execute(httpPost);
//
//            // 获取状态码
//            int statusCode = execute.getStatusLine().getStatusCode();
//            System.out.println(statusCode);
//
//            // 获取结果
//            HttpEntity entity = execute.getEntity();
//            result = EntityUtils.toString(entity, "utf-8");
//            System.out.println(result);
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            if (httpClient != null) {
//                try {
//                    httpClient.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        return result;
//    }
}
